package com.yjz.ui.banner;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.yjz.ui.R;
import com.yjz.ui.banner.core.BannerDelegate;
import com.yjz.ui.banner.core.BannerModel;
import com.yjz.ui.banner.core.IBanner;
import com.yjz.ui.banner.core.IBannerBindAdapter;
import com.yjz.ui.banner.indicator.BannerIndicator;

import java.util.List;

/**
 * 作用 : 轮播图layout
 * 作者 : yjz
 * 时间 : 2024/10/21 15:00
 */
public class BannerLayout extends FrameLayout implements IBanner {

    private BannerDelegate delegate;

    public BannerLayout(@NonNull Context context) {
        this(context, null);
    }

    public BannerLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BannerLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        delegate = new BannerDelegate(context, this);
        initCustomAttr(context, attrs);
    }

    private void initCustomAttr(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Bannerlayout);
        boolean autoPlay = typedArray.getBoolean(R.styleable.Bannerlayout_autoPlay, true);
        boolean loop = typedArray.getBoolean(R.styleable.Bannerlayout_loop, false);
        int intervalTime = typedArray.getInteger(R.styleable.Bannerlayout_intervalTime, -1);
        setAutoPlay(autoPlay);
        setLoop(loop);
        setIntervalTime(intervalTime);
        typedArray.recycle();
    }

    @Override
    public void setBannerData(int layoutResId, @NonNull List<? extends BannerModel> models) {
        delegate.setBannerData(layoutResId, models);
    }

    @Override
    public void setBannerData(@NonNull List<? extends BannerModel> models) {
        delegate.setBannerData(models);
    }

    @Override
    public void setBannerIndicator(BannerIndicator indicator) {
        delegate.setBannerIndicator(indicator);
    }

    @Override
    public void setAutoPlay(boolean autoPlay) {
        delegate.setAutoPlay(autoPlay);
    }

    @Override
    public void setLoop(boolean loop) {
        delegate.setLoop(loop);
    }

    @Override
    public void setIntervalTime(int intervalTime) {
        delegate.setIntervalTime(intervalTime);
    }

    @Override
    public void setBindAdapter(IBannerBindAdapter bindAdapter) {
        delegate.setBindAdapter(bindAdapter);
    }

    @Override
    public void setOnPageChangeListener(ViewPager.OnPageChangeListener onPageChangeListener) {
        delegate.setOnPageChangeListener(onPageChangeListener);
    }

    @Override
    public void setScrollDuration(int duration) {
        delegate.setScrollDuration(duration);
    }

    @Override
    public void setOnBannerClickListener(OnBannerClickListener onBannerClickListener) {
        delegate.setOnBannerClickListener(onBannerClickListener);
    }
}
